package com.css.util;


import java.io.*;
import java.util.Properties;

/**
 * <p>
 * -
 * </p>
 *
 * @author css
 * @since 2019/4/22
 */
public class LocalConfig {
    private static final String GLOBAL_CONFIG_FILE = "forwarding.properties";  // 此处输入文件名
    public static Properties globalConf;  // 新建Properties类的引用

    static {
        try {
            globalConf = new Properties();  // Properties对象实例化
//            // 通过类加载器获取配置文件字节流
            InputStream rankConfStream = new FileInputStream(GLOBAL_CONFIG_FILE);
            // 将配置文件装载到Properties类中
            globalConf.load(rankConfStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getProperty(String key) {
        String property = globalConf.getProperty(key);
        if (StringUtil.isEmpty(property)) {
            return "";
        }
        return property;
    }

    public static void setProperty(String key, String value) {
        try {
            OutputStream out = new FileOutputStream(new File(GLOBAL_CONFIG_FILE));
            globalConf.setProperty(key, value);
            globalConf.store(out, "Update " + key + " name");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
